//
//  HomeViewArtGalleryVC.m
//  jingdongfang
//
//  Created by MTiMac01 on 2017/8/18.
//  Copyright © 2017年 LJL. All rights reserved.
//

#import "HomeViewArtGalleryVC.h"
#import "HomeViewArtGalleryCell.h"
#import "ArtGalleryVC.h"
#import "IGArtGalleryListApi.h"
@interface HomeViewArtGalleryVC ()
@property (nonatomic, strong) IGArtGalleryListApi *api;
@property (nonatomic, copy) NSString *pageNumber;
@property (nonatomic, copy) NSString *pageSize;
@property (nonatomic, copy) NSString *queryTime;
@property (nonatomic, strong) NSMutableArray *dataSource;
@end

@implementation HomeViewArtGalleryVC

- (instancetype)initWithStyle:(UITableViewStyle)style
{
    return [super initWithStyle:UITableViewStyleGrouped];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
//    self.title = @"艺术馆";
    self.title = NSLocalizedString(@"Museum", nil);
    _pageSize = @"10";
    _pageNumber = @"0";
    _queryTime = @"";
    self.api = [[IGArtGalleryListApi alloc] initWithPageNumber:_pageNumber pageSize:_pageSize queryTime:_queryTime];
    self.dataSource = [@[] mutableCopy];
    self.tableView.backgroundColor = [UIColor c2Color];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, 10)];
    headerView. backgroundColor = [UIColor c2Color];
    self.tableView.tableHeaderView = headerView;
   if ([[[UIDevice currentDevice] systemVersion] floatValue]>= 11.0)
   {
    self.tableView.estimatedRowHeight = 0;
    self.tableView.estimatedSectionHeaderHeight = 0;
    self.tableView.estimatedSectionFooterHeight = 0;
   }
    [self.tableView ig_setHeaderWithRefreshingBlock:^{
        _pageSize = @"10";
        _pageNumber = @"0";
        _queryTime = @"";
        [self dataRequest];
    }];
    [self.tableView registerClass:[HomeViewArtGalleryCell class] forCellReuseIdentifier:@"HomeViewArtGalleryCell"];
    [self dataRequest];

}

- (void)dataRequest
{
    _api.pageSize = _pageSize;
    _api.pageNumber = _pageNumber;
    _api.queryTime = _queryTime;
    @KWeakObj(self)
    [self.api startWithDecodeClass:[IGMuseumListResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        [selfWeak.tableView ig_endHeaderRefreshing];
        [selfWeak.tableView ig_endFooterRefreshing];
        if (responseObject)
        {
            IGMuseumListResp *resp = responseObject;
            IGMuseumListModel *model = resp.data;
            if (model.museumList && model.museumList.count  > 0)
            {
                [selfWeak setupFooter];
            }
            else
            {
                [selfWeak.tableView ig_endFooterRefreshingWithNoMoreData];
            }
            _queryTime =  model.queryTime;
            if ([_pageNumber isEqualToString:@"0"])
            {
                _dataSource = [NSMutableArray arrayWithArray:model.museumList];
            }
            else
            {
                [_dataSource addObjectsFromArray:model.museumList];
            }
            [selfWeak.tableView reloadData];
        }
        else
        {
//            [selfWeak.tableView ig_endFooterRefreshingWithNoMoreData];
            //空白页处理逻辑写在这里
        }
    }];
}

- (void)setupFooter
{
    @KWeakObj(self)
    [self.tableView ig_setFooterWithRefreshingBlock:^{
        _pageNumber = [NSString stringWithFormat:@"%ld", [_pageNumber integerValue] + 1];
        [selfWeak dataRequest];
    }];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataSource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HomeViewArtGalleryCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomeViewArtGalleryCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.museum = _dataSource[indexPath.section];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ArtGalleryVC *artVC = [[ArtGalleryVC alloc] init];
    IGMuseum *museum = _dataSource[indexPath.section];
    artVC.aId = museum.museumId;
    [UIViewController ig_pushViewController:artVC animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return (KSCREENWIDTH - 20) * 30 / 71;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.1f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}
@end
